package com.hctforgreen.greenservice.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;

public class DecodeBitmapUtil {
	public Bitmap decodeBitmap(final String cache_path){
		return decodeBitmap(cache_path, 256*256);
	}
	/**
	 * 返回水平方向的bitmap用于显示
	 * 
	 * 
	 * @param filePath
	 * @return
	 */
	public Bitmap getHorizontalBitmap(String filePath) {
		return loadBitmap(filePath, true);
	}
	public Bitmap loadBitmap(String imgpath, boolean adjustOritation) { 
		if (!adjustOritation) { 
			return decodeBitmap(imgpath, 512 * 512); 
		
		} else { 
			Bitmap bm = decodeBitmap(imgpath, 512 * 512);  
			if(bm.getHeight() > bm.getWidth()){

				return adjustPhotoRotation(bm, 90);
			}
			return bm;
		} 
	} 
	/**
	 * 选择Bitmap
	 * @param bm
	 * @param orientationDegree
	 * @return
	 */
	private static Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree)
	{

		Matrix m = new Matrix();
        m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
        float targetX, targetY;
        if (orientationDegree == 90) {
        	targetX = bm.getHeight();
        	targetY = 0;
        } 
        else {
        	targetX = bm.getHeight();
        	targetY = bm.getWidth();
        }
	        
	    final float[] values = new float[9];
	    m.getValues(values);

	    float x1 = values[Matrix.MTRANS_X];
	    float y1 = values[Matrix.MTRANS_Y];

	    m.postTranslate(targetX - x1, targetY - y1);

	    Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);
	    Paint paint = new Paint();
	    Canvas canvas = new Canvas(bm1);
	    canvas.drawBitmap(bm, m, paint);

	    return bm1;
	}
	/**
	 * 获取等比缩放后的bitmap,用于替代BitmapFactory.decodeFile(String pathName)方法
	 * 
	 * @param cache_path
	 * @return
	 */
	public Bitmap decodeBitmap(final String cache_path, int maxNumOfPixels) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		/**
		 * 设置inJustDecodeBounds为true后，decodeFile并不分配空间，但可计算出原始图片的长度和宽度
		 */
		options.inJustDecodeBounds = true;
		/**
		 * 通过这个bitmap获取图片的真实px宽、高
		 */
		BitmapFactory.decodeFile(cache_path, options);

		options.inSampleSize = computeSampleSize(options, -1, maxNumOfPixels);
		options.inJustDecodeBounds = false;
		
		/**
		 * 注意这次要把options.inJustDecodeBounds 设为 false, 这次图片是要读取出来分配内存空间的。
		 */
		Bitmap bitmap = BitmapFactory.decodeFile(cache_path, options);
		
		return bitmap;
	}

	/**
	 * 计算缩放倍数
	 * @param options
	 * @param minSideLength
	 * @param maxNumOfPixels
	 * @return
	 */
	private int computeSampleSize(BitmapFactory.Options options,

	int minSideLength, int maxNumOfPixels) {
		int initialSize = computeInitialSampleSize(options, minSideLength,
				maxNumOfPixels);
		int roundedSize;
		if (initialSize <= 8) {
			roundedSize = 1;
			while (roundedSize < initialSize) {
				roundedSize <<= 1;
			}
		} else {
			roundedSize = (initialSize + 7) / 8 * 8;
		}
		return roundedSize;
	}

	private int computeInitialSampleSize(BitmapFactory.Options options,

	int minSideLength, int maxNumOfPixels) {
		double w = options.outWidth;
		double h = options.outHeight;
		int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
				.sqrt(w * h / maxNumOfPixels));
		int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
				Math.floor(w / minSideLength), Math.floor(h / minSideLength));
		if (upperBound < lowerBound) {
			// return the larger one when there is no overlapping zone.
			return lowerBound;
		}
		if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
			return 1;
		} else if (minSideLength == -1) {
			return lowerBound;
		} else {
			return upperBound;
		}

	}
}
